package com.baselibrary.libs.View.My;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.baselibrary.libs.Utils.log.LogUtils;

/**
 * Created by 35876 于萌萌
 * 创建日期: 21:57 . 2017年01月30日
 * 描述:它里面的第一个View 可以根据下拉 看到里面的内容  上推的时候  会跟着走
 * <p>
 * <p>
 * 备注:
 */

public class MyRelativeLayout_pull_mode_02 extends RelativeLayout {
    //当前按下的时候的Y轴的数值
    private int mDownY;
    //移动的Y轴数值
    private int mMoveY;
    private int mHeight = -1;
    private ViewGroup.LayoutParams mParams;
    private View mChildAt;
    private int mI;
    private int mInt;
    private int mMChildAtPaddingTop = -1;
    private int mTop;

    public MyRelativeLayout_pull_mode_02(Context context) {
        super(context);
        initView(context);
    }



    public MyRelativeLayout_pull_mode_02(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyRelativeLayout_pull_mode_02(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        if (null==getChildAt(0)){
            return;
        }


//        int left = mChildAt.getPaddingLeft();
//        int top = mChildAt.getPaddingTop();
//        int right = mChildAt.getPaddingRight();
//        int bottom = mChildAt.getPaddingBottom();
//
//        LogUtils.i("默认高度是"+ mHeight1 +"设置之后的高度是\t"+"\tleft\t"+left+"\ttop\t"+top+"\tright\t"+right+"\tbottom\t"+bottom);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mChildAt = getChildAt(0);
        mParams = mChildAt.getLayoutParams();

        if (-1 == mHeight){
            //原始高度
            mHeight = mChildAt.getMeasuredHeight();
            mChildAt.setPadding(0,-mHeight,0,0);
            //原始viewpadding 高度
            mMChildAtPaddingTop = mChildAt.getPaddingTop();

            mParams.height = 0;
            mChildAt.setLayoutParams(mParams);
        }

      //  mChildAt.invalidate();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {



        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:

                mDownY = (int) ev.getY();
                mMoveY = 0;
                LogUtils.i("MyRelativeLayout 按下Y坐标是"+ mDownY);
                break;
            case MotionEvent.ACTION_MOVE:
                //本次的移动差值
                mI = 0;
                if (mMoveY!=0){
                    mI = (int) ev.getY() - mMoveY;
                }
                LogUtils.i("移动之前的数"+mMoveY+"移动之后的数"+(int) ev.getY()+"中间相差的值是"+ mI);

                if (mI >0){
                    LogUtils.i("移动方向 下拉了"+ mI);

                }else {
                    LogUtils.i("移动方向 上拉了"+ mI);
                }

                mMoveY =  (int) ev.getY();
                LogUtils.i("按下的是\t"+mMoveY+"\t移动的是\t"+mMoveY+"这里面的差值是\t"+(mMoveY-mDownY));
                if (Math.abs(mMoveY-mDownY)>50){

                    chenglayout(mI);
                }
                LogUtils.i("MyRelativeLayout 移动Y坐标是"+mMoveY);
                //
                break;
            case MotionEvent.ACTION_UP:
                mDownY = (int) ev.getY();
                LogUtils.i("动作结束后的高度"+ mInt);
                    if (mInt<30){
                        mParams.height = 0;
                        mChildAt.setLayoutParams(mParams);
                    }
                    if (mHeight-mInt<30){
                        mParams.height = mHeight;
                        mChildAt.setLayoutParams(mParams);
                    }


                LogUtils.i("MyRelativeLayout 抬起Y坐标是"+ mDownY);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private void chenglayout(int currentY) {

        //当前高度
        mInt = mChildAt.getMeasuredHeight();
        //当前top 高度
        mTop = mChildAt.getPaddingTop();
        LogUtils.i("当前的view 里面内容高度是"+mTop);
        LogUtils.i("控件高度"+ mInt +"进来需要改变的高度是"+currentY);
        if (mInt ==0&&currentY==-1){
            //全都收起来 类似原始状态
            LogUtils.i("特殊状况我进来了");
            mParams.height = 0;
            mChildAt.setLayoutParams(mParams);
            mChildAt.setPadding(0,mMChildAtPaddingTop,0,0);
            LogUtils.i("设置完了的高度是"+ mMChildAtPaddingTop);
            return;
        }
        if (currentY>0){
            //如果 最大值了
            if (mInt +currentY>mHeight){
                mParams.height = mHeight;
                mChildAt.setLayoutParams(mParams);
                mChildAt.setPadding(0,0,0,0);
                return;
            }else{
                //如果不是最大值
                mParams.height = mInt +currentY;
                mChildAt.setLayoutParams(mParams);
                mChildAt.setPadding(0,mTop +currentY,0,0);
                return;
            }
        }
        if (currentY<0){
            //如果是最小值了
            if (mInt +currentY<=0){
                mParams.height = 0;
                mChildAt.setLayoutParams(mParams);
                mChildAt.setPadding(0,mMChildAtPaddingTop,0,0);
                return;
            }else{
                //如果不是最小值了
                mParams.height = mInt +currentY;
                mChildAt.setLayoutParams(mParams);
                mChildAt.setPadding(0,mTop+currentY,0,0);
                return;
            }
        }


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {



        return super.onTouchEvent(event);
    }
}
